package com.sf.fy.uav.order.util;

import com.sf.fy.uav.common.utils.PhoneEncoderUtil;
import com.sf.fy.uav.order.entity.OrderInfo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.*;
@Component
@RequiredArgsConstructor
public class BatchDecryptionUtil {
   private final PhoneEncoderUtil phoneEncoderUtil;
   public Map<String, String> batchDecryption(List<OrderInfo> orderInfoList,Map<String, String> userIdAndPhoneMap) {
      if (CollectionUtils.isEmpty(orderInfoList)) {
         throw new RuntimeException("订单信息不能为空！");
      }
      if (CollectionUtils.isEmpty(userIdAndPhoneMap)) {
         throw new RuntimeException("用户信息不能为空！");
      }
      Set<String> allPhoneSet = new HashSet<>(10);
      orderInfoList.forEach(orderInfo -> {
         if (StringUtils.isNotBlank(userIdAndPhoneMap.get(orderInfo.getUserId()))) {
            allPhoneSet.add(userIdAndPhoneMap.get(orderInfo.getUserId()));
         }
         if (StringUtils.isNotBlank(orderInfo.getSenderTel())) {
            allPhoneSet.add(orderInfo.getSenderTel());
         }
         if (StringUtils.isNotBlank(orderInfo.getReceiverTel())) {
            allPhoneSet.add(orderInfo.getReceiverTel());
         }
      });
      HashMap<String, String> allPhoneEncrypAndDecrypHashMap = new HashMap<>(10);
      if (!CollectionUtils.isEmpty(allPhoneSet)) {
         allPhoneSet.forEach(phone -> allPhoneEncrypAndDecrypHashMap.put(phone, phoneEncoderUtil.decryptString(phone)));
      }
      return allPhoneEncrypAndDecrypHashMap;
   }
}